﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoffeeMobile.Models.ViewModels;
using CoffeeMobile.Models;

namespace CoffeeMobile.Controllers
{
    public class TablesController : BaseController
    {
        private TableGroupsRepository _tableGroupsRepository = new TableGroupsRepository();
        public ActionResult Create()
        {
            ViewBag.Title = "Thêm mới bàn ăn";
            List<TableGroupModel> tgList = _tableGroupsRepository.GetTableGroups();
            if (tgList.Count > 0)
            {
                ViewBag.TableGroups = tgList;
                AddNewTableModel model = new AddNewTableModel();
                return View(model);
            }
            else
            {
                ViewBag.ErrorMessage = "Bạn phải có ít nhất 1 khu vực đặt bàn ăn";
                return RedirectToAction("Index");
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(AddNewTableModel model)
        {
            try
            {
                Table entity = new Table();
                if (ModelState.IsValid)
                {
                    entity.ID = Guid.NewGuid();
                    entity.Name = model.Name;
                    entity.TableGroupID = model.TableGroupID;
                    entity.CreateDate = DateTime.Now;
                    _dbLinq.Tables.InsertOnSubmit(entity);
                    _dbLinq.SubmitChanges();
                }
                ViewBag.Title = "Thêm mới bàn ăn";
                return RedirectToAction("Index");
            }catch{
                ViewBag.TableGroups = _tableGroupsRepository.GetTableGroups();
                return View(model);
            }
        }
        //
        // GET: /Tables/

        public ActionResult Index()
        {
            ViewBag.Title = "Danh mục bàn ăn";
            ViewBag.TableGroupModels = _tableGroupsRepository.GetTableGroups();
            return View();
        }
        public ActionResult Details(Guid id)
        {
            ViewBag.TableGroupModels = _tableGroupsRepository.GetTableGroups();
            TableModel model = _dbLinq.Tables.Where(p => p.ID == id).Select(p => new TableModel { ID = p.ID, Name = p.Name }).FirstOrDefault();
            model.OrderHistoryModels = _dbLinq.OrderHistories.Where(p => p.IsCleared == false && p.TableID == model.ID).Select(p => new OrderHistoryModel
            {
                FoodID = p.FoodID,
                FoodName = _dbLinq.Foods.Where(f => f.ID == p.FoodID).FirstOrDefault().Name,
                FoodUnitPrice = _dbLinq.Foods.Where(f => f.ID == p.FoodID).FirstOrDefault().UnitPrice,
                FoodImg = _dbLinq.Foods.Where(f => f.ID == p.FoodID).FirstOrDefault().Image,
                FoodQuality = (int)p.Quality,
            }).ToList();
            return View(model);
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Order(Guid id)
        {
            ViewBag.TableGroupModels = GetTableGroups();
            TableModel model = _dbLinq.Tables.Where(p => p.ID == id).Select(p => new TableModel { ID = p.ID, Name = p.Name }).SingleOrDefault();
            ViewBag.FoodGroupList = GetFoodGroups();
            return View(model);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Order(Guid id, Guid tableid, FormCollection collection)
        {
            ViewBag.TableGroupModels = GetTableGroups();
            TableModel model = _dbLinq.Tables.Where(p => p.ID == tableid).Select(p => new TableModel { ID = p.ID, Name = p.Name }).SingleOrDefault();
            ViewBag.FoodGroupList = GetFoodGroups();
            try
            {
                OrderHistory entity = _dbLinq.OrderHistories.Where(p => p.FoodID == id && p.TableID == tableid && p.IsCleared == false).FirstOrDefault();
                if (entity == null)
                {
                    entity = new OrderHistory
                   {

                       FoodID = id,
                       TableID = tableid,
                       OrderDate = DateTime.Now,
                       Quality = int.Parse(collection["number"]),
                       IsCleared = false
                   };
                    _dbLinq.OrderHistories.InsertOnSubmit(entity);
                }
                else
                {
                    entity.Quality = entity.Quality + int.Parse(collection["number"]);
                }
                _dbLinq.SubmitChanges();

                return View(model);
            }
            catch
            {
                return View(model);
            }


        }
        public List<TableGroupModel> GetTableGroups()
        {
            List<TableGroupModel> list = _dbLinq.TableGroups.Select(p => new TableGroupModel { ID = p.ID, Name = p.Name }).ToList();
            foreach (TableGroupModel t in list)
            {
                t.Tables = _dbLinq.Tables.Where(p => p.TableGroupID == t.ID).Select(p => new TableModel { ID = p.ID, Name = p.Name }).ToList();
            }
            return list;
        }
        public List<FoodGroupModel> GetFoodGroups()
        {
            List<FoodGroupModel> fgList = (from f in _dbLinq.FoodGroups
                                           select new FoodGroupModel
                                           {
                                               ID = f.ID,
                                               Name = f.Name
                                           }).ToList();
            foreach (FoodGroupModel item in fgList)
            {
                item.FoodModels = (from f in _dbLinq.Foods
                                   where f.FoodGroupID == item.ID
                                   select new FoodModel
                                   {
                                       ID = f.ID,
                                       Name = f.Name
                                   }).ToList();
            }
            return fgList;
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CacelFood(Guid foodid, Guid tableid, FormCollection collection)
        {

            try
            {

                int foodquality = int.Parse(collection["number"]);
                OrderHistory entity = (from p in _dbLinq.OrderHistories
                                       where p.IsCleared == false && p.TableID == tableid && p.FoodID == foodid
                                       select p).FirstOrDefault();
                if (foodquality == entity.Quality)
                {
                    _dbLinq.OrderHistories.DeleteOnSubmit(entity);
                }
                else
                    entity.Quality = entity.Quality - foodquality;
                _dbLinq.SubmitChanges();
                ViewBag.TableGroupModels = GetTableGroups();

                return View("Details", GetTableDetails(tableid));
            }
            catch
            {
                ViewBag.TableGroupModels = GetTableGroups();
                return View("Details", GetTableDetails(tableid));
            }
        }
        public TableModel GetTableDetails(Guid tableid)
        {
            TableModel result = _dbLinq.Tables.Where(p => p.ID == tableid).Select(p => new TableModel { ID = p.ID, Name = p.Name }).SingleOrDefault();
            result.OrderHistoryModels = _dbLinq.OrderHistories.Where(p => p.IsCleared == false && p.TableID == result.ID).Select(p => new OrderHistoryModel
            {
                FoodID = p.FoodID,
                FoodName = _dbLinq.Foods.Where(f => f.ID == p.FoodID).FirstOrDefault().Name,
                FoodUnitPrice = _dbLinq.Foods.Where(f => f.ID == p.FoodID).FirstOrDefault().UnitPrice,
                FoodImg = _dbLinq.Foods.Where(f => f.ID == p.FoodID).FirstOrDefault().Image,
                FoodQuality = (int)p.Quality,
            }).ToList();
            return result;
        }

        public ActionResult Clear(Guid tableid)
        {
            List<OrderHistory> list = (from p in _dbLinq.OrderHistories
                                       where p.TableID == tableid && p.IsCleared == false
                                       select p).ToList();
            foreach (var item in list)
            {
                item.IsCleared = true;
            }
            _dbLinq.SubmitChanges();
            return View("Details", GetTableDetails(tableid));
        }
    }
}
